package work.timejay.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @className:ApiResult.java
 * @author:王文杰
 * @version:1.0.0
 * @description:同一返回
 * @createTime 2022年11月17日 16:43:00
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ApiResult<T> {
    private Integer code;
    private Object data;
    private Object desc;
    private Object descCode;

    private static Integer SUCCESS_CODE = 200;
    private static Integer ERROR_CODE = 500;
    private static String SUCCESS_DESC = "请求成功";
    private static String ERROR_DESC = "请求失败";

    /**
     * 成功
     */
    public static <T> ApiResult<T> success(T data) {
        return returnApiResult(data, SUCCESS_CODE, SUCCESS_DESC,null);
    }

    /**
     * 成功
     */
    public static <T> ApiResult<T> success() {
        return returnApiResult(null, SUCCESS_CODE, SUCCESS_DESC,null);
    }

    private static <T> ApiResult<T> returnApiResult(
            T data, Integer successCode, Object successDesc, Object msgCode
    ) {
        ApiResult<T> apiResult = new ApiResult<>();
        apiResult.setCode(successCode);
        apiResult.setDesc(successDesc.toString());
        apiResult.setData(data);
        apiResult.setDescCode(msgCode);
        return apiResult;
    }

    /**
     * 失败
     */
    public static <T> ApiResult<T> error(Object msg,Object msgCode) {
        return returnApiResult(null, ERROR_CODE, msg, msgCode);
    }

    /**
     * 失败
     */
    public static <T> ApiResult<T> error() {
        return returnApiResult(null, ERROR_CODE, ERROR_DESC,null);
    }
}
